package com.jin.ks;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;


public class AddData extends AppCompatActivity {
    private EditText edit_name, edit_kuaiId, edit_fan, edit_follow, edit_moment, edit_like, edit_photo, edit_text, edit_birthday, edit_today_follow;
    private Button but;
    private UtilDao dao;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adddata);
        //初始化组件
        initWidget();
        DbUtil();

        /**
         * 点击编辑按钮传过来的值
         * 用于显示当前编辑项的数据信息
         * */
        intent = getIntent();
        String user_name = intent.getStringExtra("edit_name");
        User user = (User) intent.getSerializableExtra("user");

        if (user_name != null) {
            initEdit(user);
            Toast.makeText(this, user_name, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "为空", Toast.LENGTH_LONG).show();
        }


        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date(System.currentTimeMillis());
                //获取输入框的值
                String name = edit_name.getText().toString();
                String kuaiId = edit_kuaiId.getText().toString();
                String fan = edit_fan.getText().toString();
                String follow = edit_follow.getText().toString();
                String moment = edit_moment.getText().toString();
                String like = edit_like.getText().toString();
                String photo = edit_photo.getText().toString();
                String text = edit_text.getText().toString();
                String birthday = edit_birthday.getText().toString();
                String todayFollow = edit_today_follow.getText().toString();
                String time = simpleDateFormat.format(date);
                if (!name.equals("") && !kuaiId.equals("")) {
                    /**
                     * 数据库操作需要用到的数据
                     * 详情请查看 UtilDao 类下的 addData() 方法
                     * */
                    String[] key = {"mStatus", "mName", "mKuaiId", "mFan", "mFollow", "mMoment", "mLike", "mPhoto", "mText", "mBirthday", "mTodayFollow", "mTime"};
                    String[] values = {"1", name, kuaiId, fan, follow, moment, like, photo, text, birthday, todayFollow, time};
                    intent = new Intent();
                    //点击添加按钮则返回 key 和 values 数组
                    intent.putExtra("key", key);
                    intent.putExtra("values", values);
                    //点击编辑按钮则返回 name 和 phone 字符串
                    intent.putExtra("value", values);
                    //intent.putExtra("kuaiId",kuaiId);

                    setResult(RESULT_OK, intent);
                    finish();
                } else if (name.equals("") || kuaiId.equals("")) {
                    finish();
                }
            }
        });
    }


    /**
     * 初始化控件
     */
    private void initWidget() {
        edit_name = findViewById(R.id.add_edit_name);
        edit_kuaiId = findViewById(R.id.add_edit_kuaiId);
        edit_fan = findViewById(R.id.add_edit_fan);
        edit_follow = findViewById(R.id.add_edit_follow);
        edit_moment = findViewById(R.id.add_edit_moment);
        edit_like = findViewById(R.id.add_edit_like);
        edit_photo = findViewById(R.id.add_edit_photo);
        edit_text = findViewById(R.id.add_edit_text);
        edit_birthday = findViewById(R.id.add_edit_birthday);
        edit_today_follow = findViewById(R.id.add_edit_today_follow);
        but = findViewById(R.id.add_but);
    }

    public void DbUtil() {
        dao = ((MyApplication) this.getApplication()).getDao();
    }

    /**
     * 初始化编辑框
     **/
    public void initEdit(User value) {
        Toast.makeText(this, value.getName(), Toast.LENGTH_LONG).show();
        User user = value;
        edit_name.setText(user.getName());
        edit_kuaiId.setText(user.getKwaiId());
        edit_fan.setText(user.getFan());
        edit_follow.setText(user.getFollow());
        edit_moment.setText(user.getMoment());
        edit_like.setText(user.getLike());
        edit_photo.setText(user.getPhoto());
        edit_text.setText(user.getText());
        edit_birthday.setText(user.getBirthday());
        edit_today_follow.setText(user.getTodayFollow());

    }

}
